<%@ taglib uri="/tags/struts-html"  prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/struts-bean"  prefix="bean"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<fmt:setBundle basename="messages"/>
<script type="text/javascript">
  		function cleanValues(row){   			  			
  			if(document.InstructionPurchaseForm.priceReception.length != undefined){
  				document.InstructionPurchaseForm.priceReception[row].value = "";
  				document.InstructionPurchaseForm.quantity[row].value = "";
  				document.InstructionPurchaseForm.referenceReception[row].value = "";
  				document.InstructionPurchaseForm.typeReception[row].selectedIndex = 0;
  				document.InstructionPurchaseForm.accountFuture[row].selectedIndex = 0;
  				document.InstructionPurchaseForm.applicationFuture[row].selectedIndex = 0;
  			}else{
  				document.InstructionPurchaseForm.priceReception.value = "";
  				document.InstructionPurchaseForm.quantity.value = "";
  				document.InstructionPurchaseForm.referenceReception.value = "";
  				document.InstructionPurchaseForm.typeReception.selectedIndex = 0;
  				document.InstructionPurchaseForm.accountFuture.selectedIndex = 0;
  				document.InstructionPurchaseForm.applicationFuture.selectedIndex = 0;
  			}
  		}
  		function validateForm(forma){
  			var retVal = false;
  			var errors = '';
  			var warnings = '';
  			var futures = forma.quantity;
  			var balances = forma.balanceQuantity;
  			var price = forma.priceReception;
  			var isMinorPrice = forma.minorPrice;
  			var priceStopLoss = forma.stopLossQuantity;
  			var priceSug = forma.priceQuantity;
  			var cornContracted = forma.cornContracted;
  			var futuresLength = futures.length;
  			var headerErrorAdd = false;
  			var headerWarningAdd = false;
  			if( futures != undefined ){
	  			if( futuresLength != undefined ){
	  				for( var index = 0; index < futuresLength; index++ ){
	  					headerErrorAdd = false;
	  					headerWarningAdd = false;
	  					if( trim(futures[index].value) != ''){
	  	  					if(parseFloat(trim(futures[index].value))>parseFloat(trim(balances[index].value))){
	  	  						errors = errors + '\t<bean:message key="label.purchases.instructions.line"/> '+(index+1)+' :\n';
	  	  						errors = errors +  '\t\t<bean:message key="error.purchases.instructions.reception.future.incorrect"/>\n';
	  	  						headerErrorAdd = true;
	  	  					}
	  	  					if( trim(price[index].value) == '' ){
		  	  					if( !headerErrorAdd ){
		  	  						errors = errors + '\t<bean:message key="label.purchases.instructions.line"/> '+(index+1)+' :\n';
		  	  					}
		  	  					errors = errors +  '\t\t<bean:message key="error.purchases.instructions.reception.price.requiered"/>\n';
	  	  					}else{
		  	  					if( parseFloat( trim( price[index].value ) ) > parseFloat( priceSug[index].value ) ){
		  	  						warnings = warnings + '\t<bean:message key="label.purchases.instructions.line"/> '+(index+1)+' :\n';
		  	  						warnings = warnings +  '\t\t<bean:message key="warning.purchases.instructions.reception.price.invalid.amount"/>\n';
			  	  					if( parseFloat( priceStopLoss[index].value ) > -1 ){
				  	  					if( parseFloat( trim( price[index].value ) ) > parseFloat( priceStopLoss[index].value ) ){
				  	  						warnings = warnings +  '\t\t<bean:message key="warning.purchases.instructions.reception.price.invalid.stoploss"/>\n';
				  	  					}
			  	  					}
		  	  					}else if( parseFloat( trim( price[index].value ) ) < parseFloat( priceSug[index].value ) &&  isMinorPrice[index].value == 'N'){
			  	  					if( !headerWarningAdd ){
			  	  						warnings = warnings + '\t<bean:message key="label.purchases.instructions.line"/> '+(index+1)+' :\n';
			  	  					}
		  	  						warnings = warnings +  '\t\t<bean:message key="warning.purchases.instructions.reception.price.invalid.minor.amount"/>\n';
		  	  					}
	  	  					}
	  					}
	  				}
	  			}else{
	  				if( trim(futures.value) != ''){
	  					if(parseFloat(trim(futures.value))>parseFloat(trim(balances.value))){
	  						errors = errors + '\t<bean:message key="label.purchases.instructions.line"/> 1 :\n';
  	  						errors = errors +  '\t\t<bean:message key="error.purchases.instructions.reception.future.incorrect"/>\n';
	  					}
	  					if( trim(price.value) == '' ){
	  	  					if( !headerErrorAdd ){
	  	  						errors = errors + '\t<bean:message key="label.purchases.instructions.line"/> 1 :\n';
	  	  					}
	  	  					errors = errors +  '\t\t<bean:message key="error.purchases.instructions.reception.price.requiered"/>\n';
  	  					}else{
  	  	  					//TODO Validar precio
  	  					}
					}
	  			}
  			}
  			if( errors != '' ){
  				alert( '<bean:message key="label.common.errors"/> : \n' + errors );
  			}else{
  				if( warnings != '' ){
  					retVal = confirm( '<bean:message key="label.common.warnings"/> : \n' + warnings + '\n<bean:message key="label.common.wish.continue"/>' );
  	  			}else{
  	  				retVal = true;
  	  			}
  			}
  			if( retVal ){
  				cornContracted.value = cornContracted.value.replace(/,/g, "");
  			}
  			return retVal;
  		}
  		
</script>
<html:form action="/savePurchaseDetailReception" onsubmit="return validateForm(this);">
	<table border="0" cellspacing="1"  bgcolor="#CCCCC0" width="500">
		<tr align="left" class="titulocolumna">
			<td class="titulocolumna" align="center" colspan="4"><bean:message key="label.purchases.instructions"/> </td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.purchases.instructions.reference"/>:</td>
			<td class="etiqueta_campo" > <bean:write name="InstructionPurchaseDetailReceptionForm" property="purchaseCreateView.reference" /> </td>
			<td class="etiqueta"><bean:message key="label.common.comments"/>:</td>
			<td rowspan="3" valign="top" class="etiqueta_campo" width="55%"><bean:write name="InstructionPurchaseDetailReceptionForm" property="purchaseCreateView.comments" /> </td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="message.futurepurchase.purchase.date.single"/></td>
			<td class="etiqueta_campo">
				<bean:write name="InstructionPurchaseDetailReceptionForm" property="purchaseCreateView.date" />
			 </td>
			<td  class="etiqueta_campo">&nbsp;</td>		
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.purchaseinstructiondetail.status"/>: </td>
			<td class="etiqueta_campo">
			    <c:choose>
			     	<c:when test="${InstructionPurchaseDetailReceptionForm.purchaseCreateView.purchaseStatus == 1}">
			     		<bean:message key="label.purchaseinstructiondetail.status.1"/>
			     	</c:when>
			     	<c:when test="${InstructionPurchaseDetailReceptionForm.purchaseCreateView.purchaseStatus == 2}">
			     		<bean:message key="label.purchaseinstructiondetail.status.2"/>
			     	</c:when>
			     	<c:when test="${InstructionPurchaseDetailReceptionForm.purchaseCreateView.purchaseStatus == 3}">
			     		<bean:message key="label.purchaseinstructiondetail.status.3"/>
			     	</c:when>
			    </c:choose>					
			</td>
			<td class="etiqueta_campo"></td>	
		</tr>
	</table>
	<br/>
	
	
	
	<table width="98%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr align="left" class="titulocolumna">
			<td class="titulocolumna" align="center" colspan="13"><bean:message key="label.purchaseinstructiondetail.list"/> </td>
		</tr>
		<tr>
				
			<td class="titulocolumna" align="center" width="4%">
				<fmt:message key="label.purchaseinstructiondetail.line"/>
			</td>	
			<td class="titulocolumna" align="center" width="24%">
				<fmt:message key="label.purchaseinstructiondetail.customer"/>
			</td>	
			<td class="titulocolumna" align="center" width="24%">
				<fmt:message key="label.purchaseinstructiondetail.product"/>
			</td>	
			<td class="titulocolumna" align="center" width="4%">
				<fmt:message key="label.purchaseinstructiondetail.quantity"/>
			</td>	
			<td class="titulocolumna" align="center" width="6%">
				<fmt:message key="label.purchaseinstructiondetail.cornquantity"/>
			</td>
			<td class="titulocolumna" align="center" colspan="2">
				<fmt:message key="label.purchaseinstructiondetail.adquired.futures"/>
			</td>
			<td class="titulocolumna" align="center" width="6%">
				<fmt:message key="label.purchaseinstructiondetail.type"/>
			</td>
			<td class="titulocolumna" align="center" width="7%">
				<fmt:message key="label.purchaseinstructiondetail.purchase.strategy"/>
			</td>
			<td class="titulocolumna" align="center" width="10%">
				<fmt:message key="label.purchaseinstructiondetail.type.strategy"/>
			</td>
			<td class="titulocolumna" align="center" width="5%">
				<fmt:message key="label.purchaseinstructiondetail.status"/>
			</td>		
		</tr>
		
		<logic:present name="PURCHASE_INSTRUCTION_DETAILS">
			<logic:notEmpty name="PURCHASE_INSTRUCTION_DETAILS">
				<c:forEach items="${PURCHASE_INSTRUCTION_DETAILS}" var="detail" varStatus="loop">
					<tr class='
					<c:if test="${param.purchaseInstructionDetailId == detail.purchaseInstructionDetailid || purchaseInstructionDetailId == detail.purchaseInstructionDetailid}">
						<c:out value="columnaAmarilla" />
					</c:if>
					<c:if test="${not (param.purchaseInstructionDetailId == detail.purchaseInstructionDetailid  || purchaseInstructionDetailId == detail.purchaseInstructionDetailid )}">
						<c:choose>
						  <c:when test="${loop.index%2 == 0}"><c:out value="columnadatoPar" />
						  </c:when>
						<c:otherwise>
						  <c:out value="columnadatoNon" />
						</c:otherwise>
						</c:choose>
					</c:if>
					'>
						<td align="center" valign="top">
							<c:if test="${not (param.purchaseInstructionDetailId == detail.purchaseInstructionDetailid  || purchaseInstructionDetailId == detail.purchaseInstructionDetailid )}">
								<a href="#>" onclick="location.href = 'goToPurchaseDetailReception.do?purchaseInstructionDetailId=<c:out value="${detail.purchaseInstructionDetailid}" />'"><img alt="<bean:message key="label.purchases.instructions.reception"/> <c:out value="${detail.row}" />" src="images/expand.gif" border="0"></a>
							</c:if>
							<c:if test="${param.purchaseInstructionDetailId == detail.purchaseInstructionDetailid || purchaseInstructionDetailId == detail.purchaseInstructionDetailid}">
								<img alt="<bean:message key="label.purchases.instructions.reception"/> <c:out value="${detail.row}" />" src="images/collapse.gif" border="0">
							</c:if>
							<fmt:formatNumber type="number" maxIntegerDigits="3" minIntegerDigits="3" value="${loop.index + 1}" pattern="###"/>
						</td>
						<td align="left" valign="top">
							<c:out value="${detail.customerView.customerId}" /> - <c:out value="${detail.customerView.shortDescription}" />
						</td>
						<td align="left">
							<table width="99%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
								<c:forEach items="${detail.productList}" var="productos" varStatus="loopInside">
									<tr class='
										<c:if test="${param.purchaseInstructionDetailId == detail.purchaseInstructionDetailid || purchaseInstructionDetailId == detail.purchaseInstructionDetailid}">
											<c:out value="columnaAmarilla" />
										</c:if>
										<c:if test="${not (param.purchaseInstructionDetailId == detail.purchaseInstructionDetailid  || purchaseInstructionDetailId == detail.purchaseInstructionDetailid )}">
											<c:choose><c:when test="${loopInside.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>
										</c:if>
										'>
										<td align="center">
											<c:out value="${productos.productId}"></c:out>
										</td>
										<td align="left">
											<c:out value="${productos.shortDescription}"></c:out>
										</td>
									</tr>
								</c:forEach>
							</table>
						</td>
						<td align="right" valign="top">
							<c:out value="${detail.itemQuantity}" />
						</td>			
						<td align="right" valign="top">
							<c:out value="${detail.cornQuantity}" />
						</td>			
						<td  width="2%" align="right" valign="top">
							<c:out value="${detail.futures}" />
						</td>
						<td  width="4%" align="right" valign="top">
							<c:out value="${detail.tons}" />
						</td>
						<td valign="top">
							<fmt:message key="label.futuretype.${detail.futureTypeId}"/>
						</td>
						<td valign="top">
							<fmt:message key="label.strategyid.${detail.instructionStrategyId}"/>
						</td>			
						<td valign="top">
							<fmt:message key="label.strategytypeid.${detail.instructionTypeId}"/>
						</td>
						<td valign="top">
							<fmt:message key="label.purchaseinstructiondetail.status.${detail.status}"/>
						</td>					
					</tr>
					<c:if test="${param.purchaseInstructionDetailId == detail.purchaseInstructionDetailid || purchaseInstructionDetailId == detail.purchaseInstructionDetailid}">
						<input type="hidden" id="customerId" name="customerId" value="<bean:write name="detail" property="customerView.customerId"/>"/>
						
						<input type="hidden" id="cornContracted" name="cornContracted" value="<bean:write name="detail" property="cornQuantity"/>"/>
						<input type="hidden" id="itemId" name="purchaseInstructionDetailId" value="<c:out value="${detail.purchaseInstructionDetailid}" />"/>
						<tr>
							<td colspan="11">
								<table id="tableDetail" width="100%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
									<tr>
										<td class="etiqueta_campo_center">&nbsp;</td>
										<td class="etiqueta_campo_center">
											<bean:message key="menu.module.corn.sub.future"/>
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.futures.price"/>
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.futures.furchase.period"/>
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.futures.furchase.application"/>												
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.futures.minor.price"/>												
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.futures.stoploss"/>								
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.reception.balance"/>
										</td>
										<td class="etiqueta_campo_center">
											&nbsp;
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.futurepurchase.query.quantity"/>												
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.futurepurchase.query.price"/>
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.futures.furchase.period"/>												
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="label.purchases.instructions.futures.furchase.application"/>												
										</td>
										<td class="etiqueta_campo_center">
											<bean:message key="message.futurepurchase.reference"/>								
										</td>
										<td class="etiqueta_campo_center">
											&nbsp;
										</td>
									</tr>
									<logic:notEmpty name="InstructionPurchaseDetailReceptionForm" property="instructionsFuturesView">
										<logic:iterate id="row" name="InstructionPurchaseDetailReceptionForm" property="instructionsFuturesView" indexId="int_i">
											<tr class="<%=((int_i.intValue()%2)==0?"columnadatoPar":"columnadatoNon")%>">
												<td align="center">	
													<%=( int_i.intValue()+1 )%>
													<input type="hidden" id="purchaseInstructionFutureId" name="receptionFuturesList[<c:out value="${int_i}"/>].purchaseInstructionFutureId" value="<bean:write name="row" property="purchaseInstructionFutureId"/>"/>
												</td>					
												<td align="right">	
													<bean:write name="row" property="quantity"/>										
												</td>
												<td align="right">
													<bean:write name="row" property="price"/>
													<input type="hidden" id="priceQuantity" name="receptionFuturesList[<c:out value="${int_i}"/>].price" value="<bean:write name="row" property="price"/>"/>
												</td>
												<td align="center">	
													<bean:write name="row" property="accountFuturePeriodDescription"/>																														
												</td>
												<td align="center">	
													<bean:write name="row" property="applicationFuturePeriodDescription"/>
												</td>
												<td align="center">
													<c:choose>
														<c:when test="${row.miniorPrice == 'N'}">
															<bean:message key="label.common.no"/>
														</c:when>
														<c:when test="${row.miniorPrice == 'Y'}">
															<bean:message key="label.common.yes"/>
														</c:when>
													</c:choose>
													<input type="hidden" id="minorPrice" name="receptionFuturesList[<c:out value="${int_i}"/>].minorPrice" value="<bean:write name="row" property="miniorPrice"/>"/>
												</td>
												<td align="center">
													<logic:notEqual name="row" property="stopLoss" value="-1">
														<bean:write name="row" property="stopLoss"/>
													</logic:notEqual>
													<input type="hidden" id="stopLossQuantity" name="receptionFuturesList[<c:out value="${int_i}"/>].stopLoss" value="<bean:write name="row" property="stopLoss"/>"/>
												</td>
												<td align="right">
													<bean:write name="row" property="balance"/>
													<input type="hidden" id="balanceQuantity" name="receptionFuturesList[<c:out value="${int_i}"/>].balance" value="<bean:write name="row" property="balance"/>"/>
												</td>
												<td align="center">
													<a href="#>" onclick='location.href="goToFuturePurchasesRealizedList.do?purchaseInstructionFutureId=<bean:write name="row" property="purchaseInstructionFutureId"/>"'>
														<img alt="<bean:message key="label.purchases.instructions.reception.list"/>" src="images/text_list_numbers.png" border="0"/>
													</a>
													<input type="hidden" name="receptionFuturesList[<c:out value="${int_i}"/>].typeReception" id="typeReception" value="<c:out value="${detail.futureTypeId}"/>"/>
												</td>
												<td align="center">
													<c:if test="${not READ_ONLY}">
														<logic:notEqual name="row" property="balance" value="0">
															<input id="quantity" type="text" style="text-align: right;" name='receptionFuturesList[<c:out value="${int_i}"/>].quantity' onfocus="this.select();" value='<c:out value="${receptionFuturesList.int_i.quantity}"/>' class="campo_texto" onfocus="this.select();" onkeydown="return (numeric(event, 0) && noPaste(event, this));" onkeyup="extractNumber(this,2,false);" size="3" >
														</logic:notEqual>
														<logic:equal name="row" property="balance" value="0">
															<input type="hidden" id="quantity" name="receptionFuturesList[<c:out value="${int_i}"/>].quantity"/>
														</logic:equal>
													</c:if>
												</td>
												<td align="center">
													<c:if test="${not READ_ONLY}">
														<logic:notEqual name="row" property="balance" value="0">
															<input id="priceReception" type="text" style="text-align: right;" name='receptionFuturesList[<c:out value="${int_i}"/>].priceReception' class="campo_texto" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,4,false);" size="5">
														</logic:notEqual>
														<logic:equal name="row" property="balance" value="0">
															<input type="hidden" id="priceReception" name="receptionFuturesList[<c:out value="${int_i}"/>].priceReception"/>
														</logic:equal>
													</c:if>
												</td>
												<td align="center">
													<c:if test="${not READ_ONLY}">
														<logic:notEqual name="row" property="balance" value="0">
															<select id="accountFuture" name='receptionFuturesList[<c:out value="${int_i}"/>].accountFuturePeriodId' class="campo_texto" style=" font-size:9px">							
																<c:forEach var="rowSelect" items="${InstructionPurchaseDetailReceptionForm.accountPeriod}"> 								
																	<option value='<c:out value="${rowSelect.periodId}" />'  <c:if test="${rowSelect.periodId == row.accountFuturePeriodId}">selected</c:if>> <c:out value="${rowSelect.periodName}" /></option>
																</c:forEach>
															</select>
														</logic:notEqual>
													</c:if>
												</td>
												<td align="center">
													<c:if test="${not READ_ONLY}">	
														<logic:notEqual name="row" property="balance" value="0">
															<select id="applicationFuture" name='receptionFuturesList[<c:out value="${int_i}"/>].applicationFuturePeriodId' class="campo_texto" style=" font-size:9px" >							
																<c:forEach var="rowSelect" items="${InstructionPurchaseDetailReceptionForm.accountPeriod}"> 								
																	<option value='<c:out value="${rowSelect.periodId}" />'  <c:if test="${rowSelect.periodId == row.applicationFuturePeriodId}">selected</c:if>><c:out value="${rowSelect.periodName}" /></option>
																</c:forEach>
															</select>
														</logic:notEqual>
													</c:if>
												</td>
												<td align="center">
													<c:if test="${not READ_ONLY}">
														<logic:notEqual name="row" property="balance" value="0">
															<input id="referenceReception" type="text" name='receptionFuturesList[<c:out value="${int_i}"/>].referenceReception' onfocus="this.select();" value='<c:out value="${receptionFuturesList.int_i.referenceReception}"/>' style="text-align: right;" class="columnanumero" size="7" />
														</logic:notEqual>
													</c:if>
												</td>
												<td align="center">
													<c:if test="${not READ_ONLY}">
														<logic:notEqual name="row" property="balance" value="0">
															<a href="#" onclick='cleanValues("<c:out value="${int_i}"/>")'>
																<img alt="<bean:message key="login.button.clear" />" src="images/erase.png" border="0">
															</a>
														</logic:notEqual>
													</c:if>
												</td>
											</tr>
										</logic:iterate>
									</logic:notEmpty>		
								</table>
							</td>
						</tr>
					</c:if>
				</c:forEach>
			</logic:notEmpty>
		</logic:present>		
		</table>
		<br/>
	<table border="0" cellspacing="1"  bgcolor="#CCCCC0" width="500">
		<tr>
			<td colspan="4" align="center" class="columnadatoPar">
				<c:if test="${not READ_ONLY}">
					<html:submit styleClass="boton"><bean:message key="label.common.save"/></html:submit>
				</c:if>
				<html:button styleClass="boton" property="backButton" onclick="location.href = 'editInstructionPurchaseHeader.do'"><bean:message key="label.common.cancel"/></html:button> 
			</td>
		</tr>
	</table>
</html:form>